package linter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import linter.POJO.DO.Posts;
import linter.POJO.DTO.PostsDTO;
import linter.custom.ResultVO;
import linter.mapper.LikesMapper;
import linter.mapper.PostsMapper;
import linter.service.PostsService;
import linter.utils.PropertyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static linter.custom.Constant.NOTE_CONSTANT_INITIAL_VALUE_DEFAULT;

/**
* @author 北煜
* @description 针对表【posts(文章内容表)】的数据库操作Service实现
* @createDate 2024-03-17 02:50:34
*/
@Service
public class PostsServiceImpl extends ServiceImpl<PostsMapper, Posts>
    implements PostsService{

    @Resource
    PostsMapper postsMapper;
    @Resource
    LikesMapper likesMapper;

    @Override
    public Integer selPosts(Integer postsId){
        return postsMapper.selPosts(postsId);
    }

    @Override
    public boolean updateLikes(Integer postsId, Long uuid){
        Integer flag = likesMapper.selUuidLikes(uuid, postsId);
        if (flag != null){ return false; }
        return postsMapper.updateByLikes(postsId);
    }

    @Override
    public boolean updateLike(Integer postsId) {
        return postsMapper.updateLike(postsId);
    }

    @Override
    public Posts getBlogByPostsId(Integer postsId) {
        return postsMapper.selectPostsContent(postsId);
    }

    @Override
    public Object selectPosts(PostsDTO postsDTO, Long uuid) {
        QueryWrapper<Posts> queryWrapper = new QueryWrapper<>();
        List<Posts> postsList = postsMapper.selectList(queryWrapper
                .like("title", "%" + postsDTO.getTitle() + "%").or()
                .like("id","%" + postsDTO.getId() + "%").or()
                .like("content", "%" + postsDTO.getContent() + "%")
                .eq("reviewed", 1));
        return PropertyUtils.listToList(postsList, PostsDTO.class);
    }

    @Override
    public boolean updateViews(Integer postsId) { return postsMapper.updateByViews(postsId); }

    @Override
    public ResultVO<String> reviewedPost(Integer postsId, Integer reviewedStatus) {
        System.out.println(postsMapper.selPosts(postsId));
        if (postsMapper.selPosts(postsId) != null){
            switch (reviewedStatus){
                case 1:
                    if (postsMapper.updateByReviewedStatus(postsId, reviewedStatus)){ return ResultVO.success("审核通过"); }
                    return ResultVO.error("审核状态异常");
                case 2:
                    if (postsMapper.updateByReviewedStatus(postsId, reviewedStatus)){ return ResultVO.success("审核未通过"); }
                    return ResultVO.error("审核状态异常");
                default:
                    return ResultVO.error("审核状态异常");
            }
        }
        return ResultVO.error("没有该文章信息");
    }

    @Override
    public ResultVO<PageInfo<PostsDTO>> getReviewedPosts(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PostsDTO> postsDTOList = postsMapper.getReviewedPosts();
        if (postsDTOList != null){
            PageInfo<PostsDTO> pageInfo = new PageInfo<>(postsDTOList);
            return ResultVO.success(pageInfo);
        }
        return ResultVO.error("没有该文章信息");
    }

    @Override
    public PageInfo<PostsDTO> selectPostsNotReviewed(Integer pageNum, Integer pageSize, Long uuid) {
        PageHelper.startPage(pageNum, pageSize);
        List<PostsDTO> postsDTOList = postsMapper.selectPostsNotReviewed(uuid);
        return new PageInfo<>(postsDTOList);
    }

    @Override
    public PageInfo<PostsDTO> selectBySortType(Integer pageNum, Integer pageSize, Integer sortType) {
        String type;
        if(sortType == 2){ type = "likes_nub";}
        else if(sortType == 3){ type = "views";}
        else if (sortType == 4){type = "collect_nub";}
        else if (sortType == 5){type = "comments_nub";}
        else {
            type = "create_time";
        }
        PageHelper.startPage(pageNum, pageSize);
        List<PostsDTO> list = postsMapper.selectPosts(type);
        return new PageInfo<>(list);
    }

    @Override
    public String createPosts(String title, String jsonContent, Long uuid) {
        Posts posts = new Posts();
        Date date = new Date();
        posts.setTitle(title);
        posts.setContent(jsonContent);
        posts.setUuid(uuid);
        posts.setCreateTime(date);
        posts.setUpdateTime(date);
        posts.setCollectNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        posts.setLikesNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        posts.setViews(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        posts.setCommentsNub(NOTE_CONSTANT_INITIAL_VALUE_DEFAULT);
        int flag = postsMapper.insert(PropertyUtils.doToDto(posts, Posts.class));
        if (flag > 0){
            return "创建成功";
        }
        return "创建失败";
    }

}




